import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from "rxjs/Observable";
import moment from 'moment';

@Injectable()
export class TeamMarketActivityProvider {

    constructor(public http: HttpClient) {
    }

    private getStatus(status){
        let statusStr='';
        status.forEach((status) => {
            statusStr += `${status},`;
        });
        let lastIndex=statusStr.lastIndexOf(',');
        statusStr=statusStr.substr(0,lastIndex);
        return `&status=${statusStr}`;
    }
    /**
     * 获取加保活动列表
     * @returns {Observable<any>}
     */
    getJiaBaoActivityList(params): Observable<any> {
        let url = '/campaign/v1/team/getAddInsuranceList?type=1';

        if (Array.isArray(params.status)) {
            url+=this.getStatus(params.status);
        }

        if (params.startTime) {
            url += "&starttime=" + moment(params.startTime).unix();
        }

        if (params.endTime) {
            url += "&endtime=" + moment(params.endTime).unix();
        }

        if (params.groupId) {
            url += "&groupId=" + params.groupId;
        }
        return this.http.get(url);
    }

    /**
     * 根据ID活动微信客活动详情
     * @param params
     * @returns {Observable<any>}
     */
    getJiaBaoDetail(params): Observable<any> {
        let url = '/campaign/v1/team/getAddInsuranceDetail?type=1';
        url += `&activityID=${params.activityID}&groupID=${params.groupID}&isJoin=${params.isJoin}&sort=${params.sort}`;
        return this.http.get(url);
    }

    /**
     * 微信获客活动列表数据
     */
    getWebChatActivityList(params): Observable<any> {
        let url = '/campaign/v1/team/getWeChatList?type=2';
        if (Array.isArray(params.status)) {
            url+=this.getStatus(params.status);
        }

        if (params.startTime) {
            url += "&starttime=" + moment(params.startTime).unix();
        }

        if (params.endTime) {
            url += "&endtime=" + moment((`${params.endTime} 23:59:59`)).unix();
        }

        if (params.groupId) {
            url += "&groupId=" + params.groupId;
        }
        return this.http.get(url);
    }

    /**
     * 根据ID活动微信客活动详情
     * @param params
     * @returns {Observable<any>}
     */
    getWebChatDetail(params): Observable<any> {
        let url = '/campaign/v1/team/getWeChatDetail?type=2';
        url += `&activityID=${params.activityID}&groupID=${params.groupID}&isJoin=${params.isJoin}`;
        return this.http.get(url);
    }

    /**
     * 取列表数据
     * @param params
     * @param type 1 加保 2 微信 3 增员
     * @returns {Observable<any>}
     */
    getGroupList(params, type): Observable<any> {
        let url = '/campaign/v1/team/getGroupList?type=' + type;
        if (Array.isArray(params.status)) {
            url+=this.getStatus(params.status);
        }
        if (params.startTime) {
            url += "&starttime=" + moment(params.startTime).unix();
        }

        if (params.endTime) {
            url += "&endtime=" + moment((`${params.endTime} 23:59:59`)).unix();
        }

        if (params.groupId) {
            url += "&groupId=" + params.groupId;
        }
        return this.http.get(url);
    }
}


